package com.easy.controller;

import com.easy.bean.UserBean;
import com.easy.service.LoginService;
import com.easy.util.CaptchaUtil;
import com.easy.util.Result;
import com.easy.util.SYS;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
@CrossOrigin
public class LoginController {
    @Autowired
    LoginService logins;
    @RequestMapping("/captcha")
    public Result captcha(){
        return Result.success(CaptchaUtil.getCaptchaUtil());
    }
    @PostMapping("/dologin")
    public Result login(UserBean user,@RequestParam("captcha") String captcha,@RequestParam("key") String key){
        //验证码验证
        if(!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.fail(500,"验证码错误");
        }
        //登录
        try {
            String token =logins.login(user);
            //删除掉验证码
            return Result.success(token);
        } catch (Exception e) {
            return Result.fail(500,e.getMessage());
        }

    }
    @GetMapping("logininfo")
    public Result loginInfo(HttpServletRequest request){
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        UserBean user = logins.loginInfo(token);
        return Result.success(user);
    }
}
